# encoding=utf-8
import os

from utils.kit_profile import Profile
from utils.kit_utils import Log, Function

from model.kit_data import cache
from model.server_data_parser import server_data_parser


class CustomATCmdProcesser:

    @classmethod
    def load_data(cls):
        Log.info("starting load local custom AT cmds data")
        if not os.path.isfile(Profile.custom_at_cmd_file):
            Function.json_write(Profile.custom_at_cmd_file, {})
        custom_at_cmds, err = Function.json_read(Profile.custom_at_cmd_file)
        cache.init_custom_at_cmds(custom_at_cmds)
        return custom_at_cmds, err

    @classmethod
    def add_custom_at_cmd_data(cls, data):
        tab, name, value = data
        custom_at_cmds = cache.get_custom_at_cmds()
        items = custom_at_cmds.get(tab, {})
        items.update({name: value})
        custom_at_cmds[tab] = items

        cache.init_custom_at_cmds(custom_at_cmds)
        return Function.json_write(Profile.custom_at_cmd_file, custom_at_cmds)

    @classmethod
    def del_custom_at_cmd_data(cls, data):
        tab, name, _ = data
        custom_at_cmds = cache.get_custom_at_cmds()
        items = custom_at_cmds.get(tab, {})
        if name in items:
            del items[name]
        custom_at_cmds[tab] = items

        cache.init_custom_at_cmds(custom_at_cmds)
        return Function.json_write(Profile.custom_at_cmd_file, custom_at_cmds)

    @classmethod
    def modify_custom_at_cmd_data(cls, data):
        tab, old_name, _ = data[0]
        _, new_name, new_value = data[1]
        custom_at_cmds = cache.get_custom_at_cmds()
        items = custom_at_cmds.get(tab, {})
        if old_name in items:
            del items[old_name]
        items[new_name] = new_value
        custom_at_cmds[tab] = items

        cache.init_custom_at_cmds(custom_at_cmds)
        return Function.json_write(Profile.custom_at_cmd_file, custom_at_cmds)

    @classmethod
    def modify_custom_at_cmd_tab(cls, data):
        old_tab, new_tab = data
        custom_at_cmds = cache.get_custom_at_cmds()
        new_items = custom_at_cmds.get(new_tab, {})
        new_items.update(custom_at_cmds.get(old_tab, {}))
        custom_at_cmds[new_tab] = new_items
        if old_tab in custom_at_cmds:
            del custom_at_cmds[old_tab]

        cache.init_custom_at_cmds(custom_at_cmds)
        return Function.json_write(Profile.custom_at_cmd_file, custom_at_cmds)

    @classmethod
    def del_custom_at_cmd_tab(cls, data):
        tab = data
        custom_at_cmds = cache.get_custom_at_cmds()
        if tab in custom_at_cmds:
            del custom_at_cmds[tab]

        cache.init_custom_at_cmds(custom_at_cmds)
        return Function.json_write(Profile.custom_at_cmd_file, custom_at_cmds)


class ProjectATCmdProcesser:

    @classmethod
    def load_data(cls) -> tuple:
        Log.info("starting load project AT cmds data")
        project_at_cmds, err = server_data_parser.get_project_at_cmds()
        if not project_at_cmds:
            return None, err
            # TODO server data err, use local json data
        cache.init_project_at_cmds(project_at_cmds)
        return project_at_cmds, None


class ATCmdParser:

    @classmethod
    def is_invalid_at_cmd(cls, cmd):
        if not cmd:
            return False, "invalid AT cmd"
        return True, None


class UartServer:
    pass
